package Concurrency2.Chapter05_Lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * Instruction：验证中断后的LockSupportpark
 * Author：@author MaLi
 */
public class T02_LockSupport {
    public static void main(String[] args) throws InterruptedException {
        park4interruption();
        sleep4interruption();
    }

    private static void sleep4interruption() throws InterruptedException {
        Thread thread = new Thread(() -> {
            System.out.println("测试线程开始执行...");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
                System.out.println("Exception catched.");
            }
            System.out.println("是否可以执行...");
        });
        thread.start();
        thread.interrupt();//中断掉
        thread.join();
    }

    private static void park4interruption() throws InterruptedException {
        //启动一个线程, 内部park住, 在外部中断, 看后续执行状态
        Thread thread = new Thread(() -> {
            System.out.println("测试线程开始执行...");
            LockSupport.park();
            System.out.println("是否可以执行...");
        });
        thread.start();
        thread.interrupt();//中断掉
        thread.join();
//        LockSupport.unpark(thread);
        System.out.println("Main Thread is running...");
    }
}
